﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum Direction {
    left,
    right,
    up,
    down
} 
public class FloorTrigger : MonoBehaviour {
    public Transform[] _objectArr;
    public Direction[] _directionArr;
    //是否非要垂直压在触发器上
    public bool MustBeHorizontal;
    public PlayerController _player;
    bool Opened;
	// Use this for initialization
	void Start () {
        Opened = false;
        MustBeHorizontal = false;

    }
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnTriggerEnter(Collider coll) {
        int length = _objectArr.Length;
        if (MustBeHorizontal) {
            if (_player.state != State.Vertical)
                return;
        }
        for (int i = 0; i < length; i++) {
            float degree=0f;
            Vector3 pos=new Vector3();
            Vector3 axis=new Vector3();
            if (!Opened)
            {
                switch (_directionArr[i])
                {
                    case Direction.left: degree = -180; pos = _objectArr[i].position - new Vector3(0.5f, -0.1f, 0); axis = Vector3.forward; break;
                    case Direction.right: degree = 180; pos = _objectArr[i].position - new Vector3(-0.5f, -0.1f, 0); axis = Vector3.forward; break;
                    case Direction.up: degree = -180; pos = _objectArr[i].position - new Vector3(0, -0.1f, 0.5f); axis = Vector3.right; break;
                    case Direction.down: degree = 180; pos = _objectArr[i].position - new Vector3(0, -0.1f, -0.5f); axis = Vector3.right; break;
                }
            }
            else {
                switch (_directionArr[i])
                {
                    case Direction.left: degree = 180; pos = _objectArr[i].position - new Vector3(-0.5f, 0.1f, 0); axis = Vector3.forward; break;
                    case Direction.right: degree = -180; pos = _objectArr[i].position - new Vector3(0.5f, 0.1f, 0); axis = Vector3.forward; break;
                    case Direction.up: degree = 180; pos = _objectArr[i].position - new Vector3(0, 0.1f, -0.5f); axis = Vector3.right; break;
                    case Direction.down: degree = -180; pos = _objectArr[i].position - new Vector3(0, 0.1f, 0.5f); axis = Vector3.right; break;
                }
            }
            

            _objectArr[i].transform.RotateAround(pos, axis,degree);
            
            
        }
        Opened = !Opened;
        Debug.Log(Opened);
    }
}
